package com.jams.goodox.activity.mine;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.activity.mine.buy.MineRecordActivity;
import com.jams.goodox.activity.mine.view.CourseBean;
import com.jams.goodox.net.BaseEntity;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.MineService;
import com.jams.goodox.utils.StatusBarCompat;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.utils.Utils;
import com.jams.goodox.widget.RefreshRecyclerView;
import com.jams.goodox.widget.SimpleDividerItemDecoration;
import com.jams.goodox.widget.mannger.RefreshLinearLayoutManager;
import com.jams.library.commonAdapter.RecycleCommonAdapter;
import com.jams.library.commonAdapter.RecycleCommonViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import okhttp3.RequestBody;
import retrofit2.Call;

/**
 * Created by Administrator on 2018/5/17.
 */

public class CourseActivity extends BaseActivity{

    @Bind(R.id.do_answer)
    RefreshRecyclerView doAnswer;
    @Bind(R.id.swipe_refresh)
    SwipeRefreshLayout swipe_refresh;

    private List<CourseBean> doMineQuestionBeanList;

    private int pageNum = 1;
    private int pageSize = 10;

    @Override
    protected int getLayoutId() {
        return R.layout.mine_course_activity;
    }

    @Override
    protected void init() {
        StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.main_color));//沉侵式头部
        StyleUtil.titleBackKeyWithRightTextViewListener(this, "我的课程", "新建课程", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                qStartActivityForResult(MineRecordActivity.class,null,101);
            }
        });

        doMineQuestionBeanList=new ArrayList<>();
        doAnswer.setLayoutManager(new RefreshLinearLayoutManager(this));
        doAnswer.setAdapter(new RecycleCommonAdapter<CourseBean>(this,doMineQuestionBeanList,R.layout.push_layout) {

            @Override
            public void convert(RecycleCommonViewHolder helper, final CourseBean item, int position) {
                helper.setText(R.id.nickName,"发布课程："+item.getTitle());
                helper.setText(R.id.time,Utils.formatDateTime(item.getCreateTime()));
                helper.setText(R.id.see_number,item.getClickNum()+"");
                helper.setText(R.id.money,item.getPrice()+"氧币");
            }
        });
        //doAnswer.addItemDecoration(new SimpleDividerItemDecoration(this));
        swipe_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageNum=1;
                getData();
            }
        });

        doAnswer.setLoadDataListener(new RefreshRecyclerView.LoadDataListener() {
            @Override
            public void onLoadMore() {
                pageNum++;
                getData();
            }
        });

        getData();
    }

    private void getData(){

        clearMap();
        StringBuilder pData = new StringBuilder();
        pData.append("memberId:" + SePref.getId())
                .append(",pageNum:" + pageNum)
                .append(",pageSize:" + pageSize);

        params.put("requestData", "{" + pData.toString() + "}");

        String strEntity = gson.toJson(params);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);


        enqueue(createCallService(MineService.class).teacherManager(body), new RetrofitCallbackListener<BaseEntity>() {
            @Override
            public void onSuccess(Call<BaseEntity> call, BaseEntity result) {
                if (TextUtils.isEmpty(result.getResponseData())) return;
                try{
                    JsonArray jsonArray = new JsonParser().parse(result.getResponseData()).getAsJsonArray();
                    if(pageNum==1) doMineQuestionBeanList.clear();
                    for(JsonElement jsonElement:jsonArray){
                        CourseBean message = new Gson().fromJson(jsonElement, CourseBean.class);
                        doMineQuestionBeanList.add(message);
                    }
                    if(doMineQuestionBeanList.size()<10||(doMineQuestionBeanList.size()%10)!=0||jsonArray.size()==0)doAnswer.setCanLoadMore(false);
                    else doAnswer.setCanLoadMore(true);
                    doAnswer.getAdapter().notifyDataSetChanged();
                }catch (Exception e){
                    e.printStackTrace();
                }
                swipe_refresh.setRefreshing(false);
                doAnswer.loadMoreComplete();
            }

            @Override
            public void onError(String msg) {
                swipe_refresh.setRefreshing(false);
                doAnswer.loadMoreComplete();
                //ToastUtil.show(msg);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK){
            getData();
        }
    }
}
